#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using log4net;
using xConnected.Core.Model;
using xConnected.Service;

#endregion

namespace xConnected.Web.Controllers
{
    public class TechnologyController : ApiController
    {
        private readonly TechnologyService _technologyService;

        public TechnologyController(TechnologyService technologyService)
        {
            _technologyService = technologyService;
        }

        public IEnumerable<Technology> CreateTechnology(string technologyName)
        {
            ILog log = LogManager.GetLogger(GetType());
            try
            {
                if (_technologyService.GetAll().All(t => t.Name != technologyName))
                {
                    var newTech = new Technology {Name = technologyName};
                    _technologyService.SaveOrUpdate(newTech);

                    var technologyArray = new List<Technology> {newTech};
                    return technologyArray;
                }

                var existingTech = _technologyService.GetAll().First(t => t.Name == technologyName);
                var existingTechnologyArray = new List<Technology> {existingTech};
                return existingTechnologyArray;
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
        }

        //public JsonResult List(string search = "", int pageIndex = 0, int pageSize = 10)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var page = search != ""
        //                       ? _technologyService.GetAllPagedSearched(search, pageIndex, pageSize)
        //                       : _technologyService.GetAllPaged(pageIndex, pageSize);
        //        var technologies = page.Entities.ToList().Select(t => new
        //                                                                  {
        //                                                                      t.Id,
        //                                                                      t.Created,
        //                                                                      t.Updated,
        //                                                                      t.Name
        //                                                                  });

        //        return
        //            Json(
        //                new
        //                    {
        //                        search,
        //                        page.Count,
        //                        page.CurrentPage,
        //                        page.PageSize,
        //                        page.PagesCount,
        //                        Entities = technologies
        //                    }, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
        //}


        //[System.Web.Mvc.HttpPost]
        //public JsonResult Save(Technology technology)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        _technologyService.SaveOrUpdate(technology);
        //        return Json(technology.Id, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //[System.Web.Mvc.HttpPost]
        //public JsonResult DeleteTechnology(int id)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var technology = _technologyService.GetById(id);
        //        _technologyService.Delete(technology);
        //        return Json("OK", JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + System.Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new {Result = "ERROR", ex.Message}, JsonRequestBehavior.AllowGet);
        //    }
        //}

        //public JsonResult GetTechnologiesAutocomplete(string searchTerm, int maxRows)
        //{
        //    ILog log = LogManager.GetLogger(GetType());
        //    try
        //    {
        //        var technologies = _technologyService.GetAll().OrderBy(t => t.Name).Select(t => new
        //        {
        //            t.Name,
        //            t.Id
        //        });
        //        if (searchTerm != "")
        //        {
        //            technologies =
        //                technologies.Where(l => l.Name.ToLower().Contains(searchTerm.ToLower())).Take(maxRows);
        //        }
        //        return Json(technologies, JsonRequestBehavior.AllowGet);
        //    }
        //    catch (Exception ex)
        //    {
        //        log.Error(ex.Message + Environment.NewLine + "Stack: " + ex.StackTrace, ex);
        //        return Json(new { Result = "ERROR", ex.Message }, JsonRequestBehavior.AllowGet);
        //    }
        //}
    }
}